from app import Context
from pyecharts.charts import Bar, Line, Grid, Page
from pyecharts.components import Table
from pyecharts.options import ComponentTitleOpts
import pyecharts.options as opts

class ResultAnalyse:
    
    def __init__(self) -> None:
        pass

    def draw_line_with_zoo(self, xaxis, yaxis_dict, title, height):
        # 设置画布大小
        line = Line(init_opts=opts.InitOpts(width='100%',height=height, bg_color='#FFFFFF'))
        # 设置x轴数据
        line.add_xaxis(xaxis) # 横坐标必须为str类型
        # 设置y轴数据
        min_y = 1000000
        max_y = -1000000
        for key,value in yaxis_dict.items():
            line.add_yaxis(series_name=key, y_axis=value, is_symbol_show=False)
            min_y = min(value) if min(value) < min_y else min_y
            max_y = max(value) if max(value) > max_y else max_y
        # 定义交互
        tooltip_opts = opts.TooltipOpts(is_show=True, trigger='axis', trigger_on = "mousemove | click", axis_pointer_type='line')
        # 定义标题
        title_opts = opts.TitleOpts(title=title)
        # 设置配置项
        line.set_global_opts(
            tooltip_opts = tooltip_opts, 
            title_opts=title_opts, 
            xaxis_opts=opts.AxisOpts(type_="category"),
            yaxis_opts=opts.AxisOpts(
                    min_=min_y,
                    max_=max_y,
            ),
            datazoom_opts=opts.DataZoomOpts(),
        )
        return line
    
    def draw_line_with_multiple(self, xaxis, yaxis_dict, title, height):
        # 设置画布大小
        line = Line(init_opts=opts.InitOpts(width='100%',height=height, bg_color='#FFFFFF'))
        # 设置x轴数据
        line.add_xaxis(xaxis) # 横坐标必须为str类型
        # 设置y轴数据
        min_y = 1000000
        max_y = -1000000
        for key,value in yaxis_dict.items():
            line.add_yaxis(series_name=key, y_axis=value, is_symbol_show=False)
            min_y = min(value) if min(value) < min_y else min_y
            max_y = max(value) if max(value) > max_y else max_y
        # 定义交互
        tooltip_opts = opts.TooltipOpts(is_show=True, trigger='axis', trigger_on = "mousemove | click", axis_pointer_type='line')
        # 定义标题
        title_opts = opts.TitleOpts(title=title)
        # 设置配置项
        line.set_global_opts(
            tooltip_opts = tooltip_opts, 
            title_opts=title_opts, 
            xaxis_opts=opts.AxisOpts(type_="category"),
            yaxis_opts=opts.AxisOpts(
                    min_=min_y,
                    max_=max_y,
            ),
        )
        return line

    def draw_result_html(self, account:Context.Account):
        time_list = []
        assets_list = []
        for key, value in account.assets.items():
            time_list.append(str(key))
            assets_list.append(value)
        page = Page()
        # length = len(time_list)
        # 净值
        money_line = self.draw_line_with_multiple(time_list, {"资金":assets_list}, account.aid, "300px")
        page.add(
            money_line
        )
        page.render("{}.html".format(account.aid))
        